﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FantastPlatform.Community.IService;
using FantastPlatform.Framework;
using FantastPlatform.Framework.Core;
using FantastPlatform.Community.IMode;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Data.SqlClient;
using FantastPlatform.Framework.Common;

namespace FantastPlatform.Community.UI.Service
{
    public partial class MailList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginEmployee = Session["User"] as FantastPlatform.Framework.Model.Employee;
            IMailListService mlService = ServiceFactory.CreateService<IMailListService>();
            IMailListTypeService mltService = ServiceFactory.CreateService<IMailListTypeService>();

            IMailList maillist = new FantastPlatform.Community.Model.MailList();
            switch (Action)
            {
                case "Add":
                    string data = Data;
                    MailListView view = (MailListView)JsonToEntity<MailListView>(RepairString(data));
                    if (view != null)
                    {
                        maillist.Id = view.Id;
                        maillist.Name = view.Name;
                        maillist.PhoneNumber = view.PhoneNumber;
                        maillist.QQ = view.QQ;
                        maillist.Description = view.Description;
                        //maillist.Address = view.Address;
                        maillist.MailListType = new FantastPlatform.Community.Model.MailListType();
                        maillist.MailListType.Id = view.MailListTypeId;
                    }
                    maillist.Enable = true;

                    if (maillist.Id == 0)
                    {
                        maillist.CreateDate = System.DateTime.Now;
                        mlService.Create(maillist);
                        ReturnSuccessMessage("保存成功");
                    }
                    else
                    {
                        maillist.ModifyDate = System.DateTime.Now;
                        mlService.Update(maillist);
                        ReturnSuccessMessage("更新成功");
                    }
                    break;
                case "Search":
                    int total;
                    object[] ml;
                    ConditionGroup group = new ConditionGroup();
                    Condition enableCondition = new Condition() { Field = "Enable", Type = typeof(bool).FullName, Value1 = true };
                    Condition maillistCondition = new Condition() { Field = "MailListType.PoliceMan.Id", Relation = Relations.And, Type = typeof(Int32).FullName, Value1 = LoginEmployee.Id };
                    group.Conditions.Add(enableCondition);
                    group.Conditions.Add(maillistCondition);

                    IList<IMailList> mailList = mlService.Search(group, null, PageSize, SelfPage, out total);

                    ml = (from m in mailList
                          select new
                          {
                              Id = m.Id,
                              Name = m.Name,
                              PhoneNumber = m.PhoneNumber,
                              QQ = m.QQ,
                              //Address = m.Address,
                              Description = m.Description,
                              MailListType = m.MailListType.Name,
                          }).OrderByDescending(m => m.Id).ToArray();
                    ReturnList(total, ml);
                    break;
                case "SearchByCondition":
                    string Searchjson = RepairString(Data);
                    view = JsonToEntity<MailListView>(Searchjson);
                    group = new ConditionGroup();
                    enableCondition = new Condition() { Field = "Enable", Type = typeof(bool).FullName, Value1 = true };
                    group.Conditions.Add(enableCondition);
                    maillistCondition = new Condition()
                    {
                        Field = "MailListType.PoliceMan.Id",
                        Relation = Relations.And,
                        Type = typeof(Int32).FullName,
                        Value1 = LoginEmployee.Id
                    };
                    group.Conditions.Add(maillistCondition);
                    if (view.MailListTypeId != 0)
                    {
                        Condition typeCondition = new Condition()
                        {
                            Type = typeof(int).FullName,
                            Field = "MailListType.Id",
                            Value1 = view.MailListTypeId,
                            Relation = Relations.And
                        };
                        group.Conditions.Add(maillistCondition);
                    }
                    if (view.Description != null && view.Description != "")
                    {
                        Condition keyCondition = new Condition()
                        {
                            Type = typeof(string).FullName,
                            Field = "Description",
                            Value1 = '%' + view.Description + '%',
                            Relation = Relations.And,
                            Comparison = Comparisons.Like
                        };
                        group.Conditions.Add(keyCondition);
                    }
                    mailList = mlService.Search(group, null, PageSize, SelfPage, out total);
                    ml = (from m in mailList
                          select new
                          {
                              Id = m.Id,
                              Name = m.Name,
                              PhoneNumber = m.PhoneNumber,
                              QQ = m.QQ,
                              //Address = m.Address,
                              Description = m.Description,
                              CreateDate = m.CreateDate.ToString()
                          }).OrderByDescending(m => m.Id).ToArray();
                    ReturnList(total, ml);
                    break;
                case "Get":
                    maillist = mlService.Get(Id);
                    object mlobj = new
                    {
                        Id = maillist.Id,
                        Name = maillist.Name,
                        PhoneNumber = maillist.PhoneNumber,
                        QQ = maillist.QQ,
                        //Address = maillist.Address,
                        Description = maillist.Description,
                        MailListTypeId = maillist.MailListType.Id,
                        CreateDate = maillist.CreateDate.ToString()
                    };
                    ReturnObj(mlobj);
                    break;               
                case "GetType":
                    total = 0;
                    ConditionGroup typeGroup = new ConditionGroup();
                    typeGroup.Conditions.Add(new Condition { Field = "PoliceMan.Id", Type = typeof(Int32).FullName, Value1 = LoginEmployee == null ? 0 : LoginEmployee.Id });
                    typeGroup.Conditions.Add(new Condition { Field = "Enable", Type = typeof(bool).FullName, Value1 = true, Relation = Relations.And });
                    IList<IMailListType> mailListTypeList = mltService.Search(typeGroup, null, PageSize, SelfPage, out total);
                    object[] mailArry = (from m in mailListTypeList
                                         select new
                                         {
                                             Name = m.Name,
                                             Value = m.Id
                                         }
                                            ).ToArray();
                    ReturnList(total, mailArry);
                    break;
                case "Delete":
                    if (string.IsNullOrEmpty(Data))
                    {
                        throw new Exception("传入的ID为空");
                    }
                    List<int> idList = new List<int>();
                    string[] idStrList = Data.Split(',');
                    foreach (string id in idStrList)
                    {
                        idList.Add(int.Parse(id));
                    }
                    mlService.Delete(idList);
                    ReturnSuccessMessage("删除成功");
                    break;
                case "SendMessage":

                    if (string.IsNullOrEmpty(Request["ids"]))
                    {
                        throw new Exception("传入的ID为空");
                    }
                    string ids = Request["ids"];
                    if (string.IsNullOrEmpty(Request["message"]))
                    {
                        throw new Exception("传入的ID为空");
                    }
                    string message = Request["message"];
                    idList = new List<int>();
                    idStrList = ids.Split(',');
                    DateTime sendTime = DateTime.Now;
                    int i = 0;
                    foreach (string id in idStrList)
                    {
                        maillist = mlService.Get(Int32.Parse(id));
                        sendTime = sendTime.AddSeconds(++i);
                        if (maillist != null)
                        {
                            MessageManager.SendMessage(maillist.PhoneNumber, message, LoginEmployee.Code, sendTime);
                        }
                    }
                    ReturnSuccessMessage("发送成功");
                    break;
            }
        }
        public class MailListView
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string PhoneNumber { get; set; }
            public string QQ { get; set; }
            //public string Address { get; set; }
            public int MailListTypeId { get; set; }
            public string Description { get; set; }
        }
    }
}